package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.Rest.User.vipType.FindEmpNameByNO;
import com.burgeon.backend.SHOPBACKEND.entity.Classification;
import com.burgeon.backend.SHOPBACKEND.entity.Employee;
import com.burgeon.backend.SHOPBACKEND.entity.FAB.FabEntity;
import com.burgeon.backend.SHOPBACKEND.entity.Goods;
import com.burgeon.backend.SHOPBACKEND.entity.OrderVO.OrderEntity;
import com.burgeon.backend.SHOPBACKEND.entity.Seckill;
import com.burgeon.backend.SHOPBACKEND.entity.refund.RefundVO;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendFindDataMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendGoodsMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.FabMapper;
import com.burgeon.backend.SHOPBACKEND.rest.find.FindEmp;
import com.burgeon.backend.SHOPBACKEND.rest.find.FindRefundData;
import com.burgeon.backend.SHOPBACKEND.rest.find.FindStore;
import com.burgeon.backend.SHOPBACKEND.rest.find.GetAREA;
import com.burgeon.backend.SHOPBACKEND.service.BackendFindDataService;
import com.burgeon.backend.entity.CouponType;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class BackendFindDataServiceImpl implements BackendFindDataService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private BackendFindDataMapper backendFindDataMapper;
    @Autowired
    private FabMapper fabMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BackendGoodsMapper backendGoodsMapper;

    /**
     * 秒杀搜索 A 款号 B品名
     * @param field
     * @param message
     * @param account
     * @return
     */
    @Override
    public List<Seckill> findSeckillData(String field, String message, String account) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","code");
        map.put("B","name");

        List<Seckill> data = backendFindDataMapper.findSeckillData(map.get(field), message, account);

        for (Seckill seckill : data) {
            seckill.setGoods(backendGoodsMapper.getGoodsById(seckill.getGid()));
        }

        return data;
    }

    /**
     *
     * @param field A单据编号 B原云仓订单 C退货状态 D顾客姓名 E顾客手机 F购买日期 G单据日期 H商品
     * @param value
     * @param start
     * @param account
     * @return
     */
    @Override
    public HashMap findRefundData(String field, String value, Integer start, String account) {
        HashMap<String,String> condition = new HashMap<String,String>();
        condition.put("A","YC_RET_RETAIL_ID;DOCNO");
        condition.put("B","YC_RET_RETAIL_ID;YC_RETAIL_ID;DOCNO");
        condition.put("C","YC_RET_RETAIL_ID;YC_RET_STATUS");
        condition.put("D","YC_RET_RETAIL_ID;VIP_NAME");
        condition.put("E","YC_RET_RETAIL_ID;VIP_MOBILE");
        condition.put("F","YC_RET_RETAIL_ID;RETAIL_BILLDATE");
        condition.put("G","YC_RET_RETAIL_ID;BILLDATE");
        condition.put("H","M_PRODUCT_ID;NAME");

        HashMap map = new HashMap();

        Info info = GetInfo(account);
        FindRefundData findRefundData = new FindRefundData(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            ArrayList<RefundVO> data = findRefundData.getData(start,condition.get(field),value);
            map.put("count", data.get(0).getCount());
            for (RefundVO refundVO : data) {
                Goods goods = backendGoodsMapper.findGoodsDataByCode(refundVO.getGoodsCode(), account);
                if (null != goods) {
                    refundVO.setGoodsImg(goods.getImg());
                }

                User user = userMapper.FindUserDataByPhone(refundVO.getVipPhone(), account);
                if (null != user) {
                    refundVO.setVipImg(user.getImg());
                }
                refundVO.setCount(null);
            }
            map.put("data", data);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 查找商品缓存
     * @param field A 名称  B 款号
     * @param message
     * @param account
     * @return
     */
    @Override
    public List<Classification> findGoodsCache(String field, String message, String account) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","value");
        map.put("B","name");

        return backendFindDataMapper.findGoodsCache(map.get(field), message, account);
    }

    /**
     *
     * FAB 搜索
     * @param start 50
     * @param field  A 款号 B 姓名
     * @param message
     * @param account
     * @return
     */
    @Override
    public HashMap findFABData(int start, String field, String message, String account) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","code");
        map.put("B","name");

        HashMap data = new HashMap();
        List<FabEntity> fabData = backendFindDataMapper.findFABData(start * 50, map.get(field), message, account);
        for (FabEntity fabEntity : fabData) {
            fabEntity.setImg(fabMapper.getImgData(fabEntity.getId()));
            fabEntity.setFabRelevances(fabMapper.findRelevanceByMid(fabEntity.getId()));
        }
        data.put("data",fabData);
        data.put("count",backendFindDataMapper.findFABDataCount(map.get(field), message, account));


        return data;
    }

    /**
     * 时间查找订单
     * @param account
     * @param state
     * @param field A:下单时间 B支付时间 C:发货时间 D结束时间
     * @param stateTime
     * @param endTime
     * @param start
     * @return
     */
    @Override
    public HashMap findOrderDataByTime(String account, String state, String field,
                                       String stateTime, String endTime, int start) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","order_time");
        map.put("B","pay_time");
        map.put("C","deliver_time");
        map.put("D","end_time");

        HashMap data = new HashMap();
        List<OrderEntity> orderEntities = backendFindDataMapper.findOrderDataByDate(account,
                stateTime, endTime, start*10, map.get(field),state);
        for (OrderEntity orderEntity:orderEntities){
            OrderEntity orderItem = backendFindDataMapper.findOrderItemDataOnly(orderEntity.getId());
            orderEntity.setGoodsName(orderItem.getGoodsName());
            orderEntity.setGoodsCode(orderItem.getGoodsCode());
            orderEntity.setGoodsPrice(orderItem.getGoodsPrice());
            orderEntity.setNum(orderItem.getNum());
        }
        data.put("data",orderEntities);
        data.put("count",backendFindDataMapper.findOrderDataByDateCount(account,
                stateTime, endTime, start, map.get(field)));


        return data;
    }

    /**
     * @param account
     * @param state
     * @param field A会员电话 B订单编号 C会员姓名
     * @param message
     * @param start
     * @return
     */
    @Override
    public HashMap findOrderData(String account, String state, String field, String message, int start) {
        HashMap<String,String> map = new HashMap<>();
        map.put("A","u.phone");
        map.put("B","o.order_id");
        map.put("C","u.name");
        HashMap data = new HashMap();
        data.put("data",backendFindDataMapper.findOrderData(account,state,map.get(field),message,start*10));
        data.put("count",backendFindDataMapper.findOrderDataCount(account,state,map.get(field),message));

        return data;
    }

    @Override
    public HashMap findVipData(Integer start, String field, String message, String account) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","name");
        map.put("B","phone");
        HashMap data = new HashMap();
        List<User> vipData = backendFindDataMapper.findVipData(start*50, map.get(field), message, account);
        Info info = GetInfo(account);
        for (User user:vipData) {
            FindEmpNameByNO findEmpNameByNO = new FindEmpNameByNO(info.getUrl(),info.getAccount(),info.getPassword());
            try {
                user.setEmp( findEmpNameByNO.getData(user.getEmp()));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        data.put("data",vipData);
        data.put("count",backendFindDataMapper.findVipDataCount(map.get(field),message,account));
        return data;
    }

    /**
     * 搜索员工
     * @param field A:NAME 姓名 B:PHONE 电话  C:C_STORE_ID;C_AREA_ID;NAME销售区域
     * @param message
     * @param account
     * @return
     */
    @Override
    public List<Employee> findEMPData(String field, String message, String account) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","NAME");
        map.put("B","PHONE");
        map.put("C","C_STORE_ID;C_AREA_ID;NAME");
        map.put("D","C_STORE_ID;NAME");
        Info info = GetInfo(account);
        FindEmp findEmp = new FindEmp(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            return findEmp.getData(map.get(field),message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 搜索门店
     * @param field A:NAME 店仓名称 B:C_CUSTOMER_ID;NAME所属经销商  C:CONTACTOR_ID;NAME联系人 D:C_STORE_ID;NAME所属店仓
     * @param message
     * @param account
     * @return
     */
    @Override
    public HashMap FindStoreData(String field, String message, String account) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","NAME");
        map.put("B","C_CUSTOMER_ID;NAME");
        map.put("C","CONTACTOR_ID;NAME");


        Info info = GetInfo(account);
        //从bos获取user数据
        FindStore findStore = new FindStore(info.getUrl(), info.getAccount(), info.getPassword());
        HashMap data;

        try {
            data = findStore.getData(map.get(field), message);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return data;
    }

    /**
     * 搜索商品
     * @param start
     * @param field  A:款号  B:标签 C:品名 D:原价 E:是否上架 F:活动名称 G:大分类id
     * @param message
     * @param account
     * @return
     */
    @Override
    public HashMap FindGoodsData(Integer start,String field, String message, String account) {
        HashMap data = new HashMap();
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","code");
        map.put("B","label");
        map.put("C","name");
        map.put("D","original");
        map.put("E","is_putaway");
        map.put("F","activity_id");
        map.put("G","classify_id");

        List<Goods> goodsData = backendFindDataMapper.findGoodsData(start*50, map.get(field), message, account);
        for (Goods goods:goodsData){
            if ("Y".equals(goods.getIsPutaway())){
                goods.setPutaway(true);
            }else {
                goods.setPutaway(false);
            }
        }
        data.put("count",backendFindDataMapper.findGoodsDataCount(map.get(field), message, account));
        data.put("data",goodsData);

        return data;
    }

    @Override
    public List<String> getAREA(String account) {
        Info info = GetInfo(account);
        GetAREA getAREA = new GetAREA(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            return getAREA.getData();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 购物券类型
     * @param field A名称 B优惠金额 C结束时间 D应用场景 1线上2线下3通用
     * @param message
     * @param account
     * @return
     */
    @Override
    public List<CouponType> getCouponType(String field,
                                          String message, String account) {

        HashMap<String,String> map = new HashMap<String,String>();

        map.put("A","name");
        map.put("B","money");
        map.put("C","datebeg");
        map.put("D","scene");
        map.put("E",null);

        return backendFindDataMapper.findCouponData(map.get(field), message, account);
    }

    private Info GetInfo(String token) {
        return accountMapper.GetAccData(token);
    }
}
